extends MoveElement


class_name VelocityElement
"""
具有速度属性的移动
"""



## 移动模式
enum MoveMode {
    None,      ## 不做任何操作(速度保持不变)
    SpeedUp,   ## 加速
    SpeedDown, ## 减速
}
## 初始化后的自动化逻辑
@export var auto_move_mode: MoveMode = MoveMode.None

@export_category("Setting")
## 摩擦系数(减速用)
@export var frictional: float = 100
## 线性加速度(加速用)
@export var acceleration: float = 100
## 转向角速度
@export_range(0, 360, 1, "radians") var palstance: float = deg_to_rad(90)

## 最小速度
@export var min_speed: float = 0
## 最大速度
@export var max_speed: float = 200
@export_category("")


func get_stand_position() -> Vector2:
    return move_component.wrapper.global_position

func speed_up(delta: float) -> float:
    return move_toward(move_component.speed, max_speed, acceleration * delta)

func speed_down(delta: float) -> float:
    return move_toward(move_component.speed, min_speed, frictional * delta)

func turn_left(delta: float) -> Vector2:
    var increment: float = palstance * delta
    return move_component.direction.rotated(-increment)

func turn_right(delta: float) -> Vector2:
    var increment: float = palstance * delta
    return move_component.direction.rotated(increment)

# 朝指定的方向转向
func turn_to(direction: Vector2, delta: float) -> Vector2:
    # 没有角速度
    if palstance <= 0:
        return direction
    # 完全停止状态不需要转向
    if move_component.is_stop():
        return direction
    var angle: float = direction.angle_to(move_component.direction)
    var increment: float = palstance * delta
    if abs(angle) < increment:
        return direction
    var turn_angle: float = move_toward(0, angle, increment)
    return move_component.direction.rotated(-turn_angle)

func yield_move(delta: float) -> Vector2:
    match auto_move_mode:
        MoveMode.None: pass
        MoveMode.SpeedUp: move_component.speed = speed_up(delta)
        MoveMode.SpeedDown: move_component.speed = speed_down(delta)
    return Vector2.ZERO
